package com.zdp.degisn.pipe_filter.filter;

import com.zdp.degisn.pipe_filter.entity.Context;

import java.util.Objects;

/**
 * @author zhengdp
 * @Description
 * @Date 2022/8/28 17:39
 * @Created by zhengdp
 */
public class AbstarctFilterChain implements FilterChain {
    // 模板类
    FilterChain next;
    Filter filter;

    public AbstarctFilterChain(FilterChain next, Filter filter) {
        this.next = next;
        this.filter = filter;
    }

    @Override
    public void handle(Context context) {
        if(context.match(filter)){
            filter.doHandle(context);
            fireNext(context);
            // 后置处理
            filter.afterHandle(context);
        }else{
            // 跳链，当前节点也不进行后置处理
            fireNext(context);
        }
    }

    @Override
    public void fireNext(Context context) {
        if(Objects.nonNull(next)){
            next.handle(context);
        }
    }
}
